import com.sun.xml.internal.stream.util.ThreadLocalBufferAllocator;

import java.util.Random;
//感受多线程的独立执行与"并发"
public class demo1 {
    static class MyThread extends Thread{
        @Override
        public void run(){
            Random random = new Random();
            while (true){
                //打印当前线程名字
                System.out.println(Thread.currentThread().getName());
                try{
                    //当前线程随机休眠一定时间
                    //注意此处的休眠不能抛出异常因为它重写的父类方法并没有抛出异常
                    // 而在下面的main方法中则可以
                    Thread.sleep(random.nextInt(10)*100);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        }
    }
    public static void main(String[] args) throws InterruptedException {
        MyThread t = new MyThread();
        t.start();
        Random random = new Random();
        while (true){
            //打印当前线程名字
            System.out.println(Thread.currentThread().getName());
            Thread.sleep(random.nextInt(10)*100);

        }
    }
}
